<template>
	<view class="todo-list">
		<view class="todo-item" v-for="(item, index) in todoList" :key="item.id"
			:class="{ 'completed-item': item.complete }">
			<text class="todo-text">{{ item.title }}</text>
			<view class="todo-actions">
				<wd-checkbox v-model="item.complete" class="todo-checkbox"></wd-checkbox>
				<wd-button icon="edit-outline" type="icon" @click="handleEdit(item, index)"></wd-button>
				<wd-button icon="delete" type="icon" @click="handleDel(item.id)" class="delete-btn"></wd-button>
			</view>
		</view>

		<view class="empty-state" v-if="todoList.length === 0">
			<view class="empty-icon">📋</view>
			<text class="empty-text">还没有待办事项</text>
			<text class="empty-hint">添加你的第一个待办吧~</text>
		</view>
	</view>
</template>

<script setup>
	import {
		onMounted,
		onUnmounted,
		ref,
		watchEffect
	} from 'vue'

	const props = defineProps({
		todos: {
			type: Array,
			default: () => []
		}
	})

	const emit = defineEmits(['update', 'delete'])

	const todoList = ref([...props.todos])

	function handleEdit(item, index) {
		emit('update', item, index)
	}

	function handleDel(id) {
		emit('delete', id)
	}

	const unWatch = watchEffect(() => {
		todoList.value = [...props.todos]
	})

	onUnmounted(() => {
		unWatch()
	})
</script>

<style>
	.todo-list {
		display: flex;
		flex-direction: column;
		gap: 16rpx;
	}

	.todo-item {
		display: flex;
		align-items: center;
		background-color: #fff;
		border-radius: 12rpx;
		padding: 24rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.03);
		transition: all 0.2s;
	}

	.todo-item.completed-item {
		background-color: #f9fafb;
	}

	.todo-text {
		flex: 1;
		font-size: 28rpx;
		color: #333;
		line-height: 1.5;
	}

	.todo-item.completed-item .todo-text {
		color: #9ca3af;
		text-decoration: line-through;
	}

	.todo-actions {
		display: flex;
		align-items: center;
		gap: 16rpx;
	}

	.todo-checkbox {
		width: 40rpx;
		height: 40rpx;
	}

	.delete-btn {
		width: 50rpx;
		height: 50rpx;
		color: #f53f3f;
	}

	.delete-btn:active {
		background-color: rgba(245, 63, 63, 0.1);
	}

	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 80rpx 0;
		color: #9ca3af;
	}

	.empty-icon {
		font-size: 80rpx;
		margin-bottom: 24rpx;
	}

	.empty-text {
		font-size: 30rpx;
		margin-bottom: 8rpx;
	}

	.empty-hint {
		font-size: 24rpx;
		color: #d1d5db;
	}
</style>